<?php

namespace App\Http\Controllers\applet_api;

use App\Http\Controllers\Controller;
use App\Models\AppVip;
use App\Models\Concern;
use App\Models\EncyclopediasCollect;
use App\Models\EncyclopediasReply;
use App\Models\InformationCollection;
use App\Models\Integral;
use App\Models\InteractionComment;
use App\Models\NodeCollection;
use App\Models\Platform_user;
use App\Models\UserInfo;
use App\Models\VipOrder;
use Illuminate\Http\Request;

class UserController extends Controller
{

    public function index(Request $Request)
    {
        $search = $Request->search;
        $skill = $Request->skill ?? 0;
        // $sort = $Request->sort;
        $is_join = $Request->is_join;
        $pass = $Request->pass;
        $limit = $Request->limit ?: 30;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;
        $sql = Platform_user::select('id','name','head_img','is_join')
                ->with(['info' => function ($query) {
                    $query->select('id', 'meta_description');
                }])
                ->when($search, function ($query, $search) {
                    return $query->where('name','like', '%'.$search.'%');
                })
                ->when($is_join, function ($query, $is_join) {
                    return $query->where('is_join',$is_join);
                })
                ->when($skill, function ($query, $skill) {
                    return $query->where('skill',$skill);
                })
                ->when($pass, function ($query, $pass) {
                    return $query->where('pass',$pass);
                });
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('id','desc')->get();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data,'total'=>$total]);
    }

    public function info(Request $Request)
    {
        $data = Platform_user::withCount('follows')->withCount('fans')->with('info')->find($Request->uid);
        $date = Date('Y-m-d',time());
        $app_vip = AppVip::orderBy('id','asc')->get();
        $data->is_vip = false;
        $data->vip_auth = '';
        foreach ($app_vip as $k => $v) {
            $app_vip[$k]->is_vip = false;
            $app_vip[$k]->due_date = '';
            $app_vip[$k]->vip = VipOrder::where('vip_id',$v->id)->where('uid',$data->id)->where('status',1)->whereDate('due_date','>=',$date)->orderBy('id','desc')->first();
            if($app_vip[$k]->vip){
                $app_vip[$k]->is_vip = true;
                $app_vip[$k]->due_date = $app_vip[$k]->vip->due_date;
                $data->is_vip = true;
                $data->vip_auth = $v->auth;
                $data->vip_name = $v->title;
            }
        }
        $data->vip = $app_vip;
        //积分
        $data->integral = Integral::where('uid',$data->id)->sum('score');


        $info = UserInfo::where('uid',$Request->uid)->first();
        if($info){
            $data->company_name = $info->company_name;
            $data->post = $info->post;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function show(Request $Request)
    {
        $data = Platform_user::withCount('follows')->withCount('fans')->with('info')->find($Request->id);
        if($Request->uid){
            $is = Concern::where('uid',$Request->uid)->where('concern_user',$Request->id)->first();
            $data->inverted = $is ? true : false;
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function collect(Request $Request)
    {
        $data = [];
        if($Request->type == 'node'){
            $data = NodeCollection::with('node')->where('uid',$Request->uid)->get();
            foreach ($data as $k => $v) {
                $data[$k]->img = '';
                $data[$k]->title = '';
                if($v->node){
                    $data[$k]->img = $v->node->img;
                    $data[$k]->title = $v->node->title;
                }
            }
        }
        if($Request->type == 'information'){
            $data = InformationCollection::with('information')->where('uid',$Request->uid)->get();
            foreach ($data as $k => $v) {
                $data[$k]->img = '';
                $data[$k]->title = '';
                if($v->information){
                    $data[$k]->img = $v->information->img;
                    $data[$k]->title = $v->information->title;
                }
            }
        }
        if($Request->type == 'encyclopedias'){
            $data = EncyclopediasCollect::with('encyclopedias')->where('uid',$Request->uid)->get();
            foreach ($data as $k => $v) {
                $data[$k]->img = '';
                $data[$k]->title = '';
                if($v->encyclopedias){
                    $data[$k]->img = $v->encyclopedias->img;
                    $data[$k]->title = $v->encyclopedias->title;
                }
            }
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function comment(Request $Request)
    {
        $data = [];
        if($Request->type == 'encyclopedias'){
            $data = EncyclopediasReply::with('encyclopedias')->where('user_id',$Request->uid)->get();
            foreach ($data as $k => $v) {
                $data[$k]->img = '';
                $data[$k]->title = '';
                if($v->encyclopedias){
                    $data[$k]->img = $v->encyclopedias->img;
                    $data[$k]->title = $v->encyclopedias->title;
                }
            }
        }

        if($Request->type == 'posts'){
            $data = InteractionComment::with('interaction')->where('user_id',$Request->uid)->get();
            foreach ($data as $k => $v) {
                $data[$k]->img = '';
                $data[$k]->title = '';
                if($v->interaction){
                    $data[$k]->img = json_decode($v->interaction->img)[0];
                    $data[$k]->title = $v->interaction->title;
                }
            }
        }
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }


    public function concern_edit(Request $Request)
    {
        $data = Concern::where('uid',$Request->uid)->where('concern_user',$Request->id)->first();
        if($data){
            $data->delete();
            return response()->json(['code' => '200', 'msg' => 'success']);
        }
        $data = new Concern();
        $data->concern_user = $Request->id;
        $data->uid = $Request->uid;
        $data->state = 1;
        $data->create_time = time();
        $data->update_time = time();
        $data->save();
        return response()->json(['code' => '200', 'msg' => 'success']);
    }

    public function integral(Request $Request)
    {
        $data = Integral::where('uid',$Request->uid)->sum('score');
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }

    public function integralStore(Request $Request)
    {
        //判断是否签到过
        $date = Date('Ymd',time());
        $data = Integral::where('uid',$Request->uid)->where('type',1)->where('content',$date)->first();
        if($data){
            return response()->json(['code' => '200', 'msg' => '今天已经签到过了']);
        }
        $data = new Integral();
        $data->uid = $Request->uid;
        $data->score = 1;
        $data->type = 1;
        $data->content = $date;
        $data->des = '签到';
        $data->create_time = time();
        $data->update_time = time();
        $data->source_id = 0;
        $data->save();
        return response()->json(['code' => '200', 'msg' => 'success','data'=>$data]);
    }
    
    
}
